package cn.toos.mybatis;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {

        // 生成代码的服务
        public static String SERVICE_NAME = "zc-curriculum/zc-curriculum-service";
        // 生成代码包位置
        public static String PARENT_PACK_NAME = "cn.zc.curriculum.service";
        // 需要生成的表
        public static String[] TABLES = {
                "interface_manager"
        };

        public static void main(String[] args){
            // 代码生成器
            AutoGenerator autoGenerator = new AutoGenerator();
            // 数据源
            DataSourceConfig dataSourceConfig = new DataSourceConfig();
            dataSourceConfig.setDbType(DbType.MYSQL);
            dataSourceConfig.setUrl("jdbc:mysql://192.168.2.14:3306/zhongcheng?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
            dataSourceConfig.setUsername("keno");
            dataSourceConfig.setPassword("123456");
            dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
            autoGenerator.setDataSource(dataSourceConfig);
            // 全局配置--配置文件生成后放的地址
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setOutputDir(System.getProperty("user.dir") + "/" + SERVICE_NAME + "/src/main/java");
            // 去掉service名字前面的I
            globalConfig.setServiceName("%sService");
            globalConfig.setOpen(false);
            globalConfig.setAuthor("keno");
            autoGenerator.setGlobalConfig(globalConfig);
            // 包信息
            PackageConfig packageConfig = new PackageConfig();
            packageConfig.setParent(PARENT_PACK_NAME);
            // 生成文件所在文件夹
            packageConfig.setModuleName(null);
            packageConfig.setController("controller");
            packageConfig.setService("service");
            packageConfig.setServiceImpl("service.impl");
            packageConfig.setMapper("mapper");
            packageConfig.setEntity("entity");
            autoGenerator.setPackageInfo(packageConfig);
            // 配置策略  配置是否使用lombok 字段转实体类命名规范，下划线转驼峰
            StrategyConfig strategyConfig = new StrategyConfig();
            strategyConfig.setInclude(TABLES);
            // 是否启用lombok
            strategyConfig.setEntityLombokModel(true);
            // 字段转驼峰
            strategyConfig.setNaming(NamingStrategy.underline_to_camel);
            strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
            strategyConfig.setRestControllerStyle(true); //restful api风格控制器
            autoGenerator.setStrategy(strategyConfig);

            autoGenerator.execute();
        }
}
